package com.pi.nbcenter.base.task.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.pi.base.enumerate.redis.RedisCacheEnum;
import com.pi.base.util.cache.RedisUtil;
import com.pi.common.http.task.Processable;
import com.pi.nbcenter.base.task.handler.param.DeviceInsWakeUpParam;
import com.pi.nbcenter.device.bean.dto.ins.base.InsParam;
import com.pi.nbcenter.device.service.pi.external.PiInsService;

@Service
public class DeviceInsExecuteTask implements Processable<DeviceInsWakeUpParam> {

    private static final Logger logger = LoggerFactory.getLogger(DeviceInsExecuteTask.class);

    @Autowired
    private PiInsService piInsService;

    @Override
    public void process(DeviceInsWakeUpParam record) {
        if (null == record || null == record.getDeviceId()) {
            logger.info("传入数据为空");
            return;
        }
        Object[] param = new Object[]{record.getDeviceId()};
        try {
            InsParam insParam = RedisUtil.lpop(RedisCacheEnum.IOT_DEV_INS_ASYNC, param);
            if (insParam != null) {
                piInsService.executeIns(insParam);
                logger.info("指令 {} 已发送: {}", insParam.getInsEnum().getDesc(), JSON.toJSONString(insParam));
            }
        } catch (Exception e) {
            logger.error("设备指令执行出错", e);
        }
    }
}
